<?php

/**
 * uniBoard Templates abstract
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-06 01:45:14 +0000 (Mon, 06 Sep 2010) $
 * $Revision: 59 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

abstract class class_templates_abstract{

	/**
	 * Array with zebras
	 *
	 * @var array
	 */
	
	private $zebras = array();
	
	// ------------------------------------------------------------------------

	/**
	 * Returns current zebra element
	 *
	 * @param string $template
	 * @param string $zebra_key
	 * @return string
	 */
	
	function zebra( $template, $zebra_key){
		
		// Zebra in array?
		if ( !isset( $this -> zebras[$template]))
		{
			// Set empty array
			$this -> zebras[$template] = array();
		}	
		
		//key in array?
		if ( !isset( $this -> zebras[$template][$zebra_key]))
		{
			// Insert zebra into array
			$this -> zebras[$template][$zebra_key] = array(
				0 => explode( '|', $zebra_key), // Array items
				1 => substr_count( $zebra_key, '|'), // Array items
				2 => 0 // Current zebra item
			);
		}

		// Return current zebra item
		return $this -> zebras[$template][$zebra_key][0][$this -> zebras[$template][$zebra_key][2]];
			
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Moves zebras forward in template
	 *
	 * @param string $template
	 */
	
	function updateZebras( $template){
		
		// Any zebras in template?
		if ( isset( $this -> zebras[$template]))
		{
			// Reiterate in-template zebras
			foreach ( $this -> zebras[$template] as $zebra_id => $zebra_data)
			{
				// Is Zebra pointer at its end?
				if ( $zebra_data[2] == $zebra_data[1])
				{
					// Rewind pointer to beginning
					$this -> zebras[$template][$zebra_id][2] = 0;
				}
				else
				{
					// Move pointer forward
					$this -> zebras[$template][$zebra_id][2] ++;
				}
			}
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * scales image down to specified size and returns width="" height=""
	 *
	 * @param integer $org_w
	 * @param integer $org_h
	 * @param integer $tar_w
	 * @param integer $tar_h
	 * @return string
	 */
	
	function scaleImage( $org_w, $org_h, $tar_w, $tar_h){
		
		//scale image
		$image_size = $this -> appCore -> scaleImage( $org_w, $org_h, $tar_w, $tar_h);
		
		//set scaling
		return 'width="'.$image_size[0].'" height="'.$image_size[1].'"';								
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * this function escapes quotes
	 *
	 * @param string $text
	 * @param integer $mode ( 0 = all quotes, 1 = single quotes only, 2 = double quotes only)
	 * @return string
	 */
	
	function doSlashing( $text, $mode = 0){

		//replace single quotes: ' becomes &#039;
		if ( $mode == 0 || $mode == 1)
			$text = str_replace( "'", "\'", $text);
		
		//replace double quotes: " becomes &quot;
		if ( $mode == 0 || $mode == 2)
			$text = str_replace( '"', '\"', $text);
		
		//return finished text
		return $text;
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * this function cuts too long strings
	 *
	 * @param string $text
	 * @param integer $desired_length
	 * @param bool $add dots ( "adds ... at the end of cutted string")
	 * @return bool
	 */
	
	function cutString( $text, $desired_length = 0, $add_dots = true){

		//cut string?
		if ( $desired_length > 0 && mb_strlen( $text) > $desired_length){
			
			//castrate
			$text = trim( class_strings::substr( $text, 0, $desired_length));
		
			//add dots?
			if ( $add_dots)
				$text .= '...';	
				
		}
		
		//return finished text
		return $text;
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Overload function for php one
	 *
	 * @param string $text
	 * @return string
	 */
	
	function urlencode( $text = ''){
		
		// Clear vars
		$text = class_strings::fixHtmlChars( $text);
		
		// Use native php function to return safe function
		return urlencode( $text);
		
	}
	
}